var hrefUrl = "";
$(function(){
	var uri = window.location.search;
	if (uri.indexOf("?") != -1) {
		var str = uri.substr(1);
		if (str.split("=")[0] == "path"){
			hrefUrl = str.substr(str.indexOf("=")+1);
		}
	}
//	hrefUrl = base.getParameter("path");
	var userID = $.cookie("userID");
	if (userID == null || userID == "null" || userID == "") {
		alert("会话已过期，请重新登录！");
		window.location.href = "login.html";
	}
	//更换头像
	var companyHeadUrl = $.cookie("companyHeadUrl");
	if (companyHeadUrl != null && companyHeadUrl != "") {
		$(".user-logo").html('<img src="'+companyHeadUrl+'" />')
	}
	//获取菜单数据
	var menuUrl = $.cookie("menuUrl");
	base.postData(base.url.getHomePageData, {userID:userID,menuUrl:menuUrl,sonMenuUrl:hrefUrl}, callInitMenu);
	//滑动到大屏时显示菜单
	Menuasidedisplay();
	$(window).resize(function(){
		setTimeout(function(){
			Menuasidedisplay();
		},500);
	});
});
function Menuasidedisplay(){
	if($(window).width()>=768){
		if ($(".menu-aside").attr("class").indexOf("hide")>0) {
			$(".menu-aside").attr("class", "menu-aside");
		}
	} 
}
$("#collapsed").on("click", function(){
	if ($(".menu-aside").attr("class").indexOf("show")>0) {
		$(".menu-aside").attr("class", "menu-aside hide");
	}else{
		$(".menu-aside").attr("class", "menu-aside show");
	}
});

//菜单栏
$(".menu-aside").on("click", ".list-group-item", function(){
//	if ($(this).attr("class").indexOf("selected") != -1) {
//		return;
//	}
	if($(window).width()<768){
		$(".menu-aside").attr("class", "menu-aside hide");
	}
	$(".menu-aside .list-group-item").removeClass("selected");
	$(this).addClass("selected");
	history.pushState(null, null, "index.html");
	if($(this).find("a").attr("data-menu") != null && $(this).find("a").attr("data-menu") != ""){
		//更新二级菜单
		var childList = $(this).find("a").attr("data-menu");
		var menuUrl = $(this).find("a").attr("data-menuUrl");
		childList = eval('(' + childList + ')');
		if($(window).width()<768){
			createPhoneTwoMenu(childList, true);
		}else{
			createTwoMenu(childList, "", menuUrl);
		}
	}else{
		//更新没有二级菜单页面
		$(".menu-aside .list-two-item").removeClass("selected");
		var path = $(this).find("a").attr("data-href");
		
		if (path.indexOf("sellerLogin") != -1) {
			window.open(base.url.mailuoLogin+"?username="+$.cookie("phone"));
//			window.location.href = base.url.mailuoLogin+"?username="+$.cookie("phone");
		} else if (path.indexOf("toLogin") != -1) {
			//安全退出
			$.cookie("userID", null, {expires: -1, path:"/"});
			$.cookie("menuUrl", null, {expires: -1, path:"/"});
			window.location.href = "login.html";
		} else{
			$.cookie("menuUrl", path, {path:"/"});//更新菜单栏点击状态
			if($(window).width()<768 && path.indexOf("myClient") != -1){
				createIframe("myClient/myClientPhone.html");
			}else{
				createIframe(path);
			}
		}
		
	}
});
$(".menu-aside").on("click", ".list-two-item", function(){
	$(".menu-aside").attr("class", "menu-aside hide");
	$(".menu-aside .list-group-item").removeClass("selected");
	$(".menu-aside .list-two-item").removeClass("selected");
	$(this).addClass("selected");
	history.pushState(null, null, "index.html");
	//更新没有二级菜单页面
	var path = $(this).find("a").attr("data-href");
	$.cookie("menuUrl", path, {path:"/"});
	if (path.indexOf("createContent") != -1) {
//		window.location.href="myExtension/createPhoneContent.html";
		initIframe("myExtension/createPhoneContent.html");
	}else if (path.indexOf("advertManage") != -1) {
		initIframe("myExtension/advertManagePhone.html");
	}else{
		initIframe(path);	
	}
});	
//二级菜单
$(".top-tab").on("click", "li", function(){
	$(".top-tab li").removeClass("active");
	$(this).addClass("active");
	var path = $(this).find("a").attr("data-href");
	$.cookie("menuUrl", path, {path:"/"});
	history.pushState(null, null, "index.html");
	initIframe(path);
});
//个人中心的功能
$(".btn-tab").on("click", "button", function(){
	$(".btn-tab button").removeClass("active");
	$(this).addClass("active");
	var path = $(this).attr("data-href");
	if (path.indexOf("login") != -1) {
		//安全退出
		$.cookie("userID", null, {expires: -1, path:"/"});
		$.cookie("menuUrl", null, {expires: -1, path:"/"});
		window.location.href = path;
	} else {
		$.cookie("menuUrl", path, {path:"/"});
		history.pushState(null, null, "index.html");
		initIframe(path);
	}
});
//初始化一级菜单
function callInitMenu(data){
	if (data.success) {
		var menuList = data.context.menuList;
		sonMenuUrl = data.context.sonMenuUrl;
		var html = '';
		for (var i = 0;i < menuList.length; i++) {
			if($(window).width()<768){
				if (menuList[i].menuUrl.indexOf("statistical") != -1 || menuList[i].menuUrl.indexOf("redPacket") != -1 || menuList[i].menuUrl.indexOf("myCenter") != -1 || menuList[i].menuUrl.indexOf("customerStatistics") != -1 || menuList[i].menuUrl.indexOf("sellerLogin") != -1) {
					continue;	
				}
			}
			var childList = "";
			if (menuList[i].childlist != null && menuList[i].childlist.length>0) {
				childList = JSON.stringify(menuList[i].childlist).replace(/\"/g,"'");	
			}
			if (menuList[i].selected == 1){
				html += '<li class="list-group-item selected"><a data-href="'+menuList[i].menuUrl+'" data-menuUrl="'+menuList[i].menuUrl+'" data-menu="'+childList+'" href="javascript:void(0)"><img src="'+menuList[i].menuImage+'" />'+menuList[i].menuName+'</a></li>';
				if (menuList[i].childlist != null && menuList[i].childlist.length>0) {
					if($(window).width()<768){
						//更新手机端二级菜单
						html += createPhoneTwoMenu(menuList[i].childlist, true);
					}else{
						createTwoMenu(menuList[i].childlist, sonMenuUrl, menuList[i].menuUrl);
					}
				}else {
					$.cookie("menuUrl", menuList[i].menuUrl, {path:"/"});
					if (sonMenuUrl != null && sonMenuUrl != "") {
						createIframe(sonMenuUrl);
					}else {
						if($(window).width()<768 && menuList[i].menuUrl.indexOf("myClient") != -1){
							createIframe("myClient/myClientPhone.html");
						}else{
							createIframe(menuList[i].menuUrl);	
						}
					}
				}
			} else{
				html += '<li class="list-group-item"><a data-href="'+menuList[i].menuUrl+'" data-menuUrl="'+menuList[i].menuUrl+'" data-menu="'+childList+'" href="javascript:void(0)"><img src="'+menuList[i].menuImage+'" />'+menuList[i].menuName+'</a></li>';
				if($(window).width()<768 && menuList[i].childlist != null && menuList[i].childlist.length>0){
					//更新手机端二级菜单
					html += createPhoneTwoMenu(menuList[i].childlist, false);
				}
			}
		}
		if ($(window).width()<768) {
			html += '<li class="list-group-item"><a data-href="toLogin" data-menuUrl="toLogin" data-menu="" href="javascript:void(0)"><img src="http://gyyxjqd.oss-cn-shenzhen.aliyuncs.com/data/1526981356854.png?Expires=1842341354&OSSAccessKeyId=LTAI6svFaQnHjOWJ&Signature=MmPvfxregaWyZj3L1EXLfMQZ1OU%3D" />安全退出</a></li>';
		}
		$(".menu-aside .list-group").html(html);
	}
}
//更新二级菜单
function createTwoMenu(childList, sonMenuUrl, menuUrl){
	var html = '';
	for (var i = 0; i < childList.length; i++) {
		if (menuUrl.indexOf("myCenter") != -1) {
			html += '<div class="btn-group" role="group">'
			if (childList[i].selected == 1){
				html += '<button type="button" data-href="'+childList[i].menuUrl+'" class="btn btn-default active">'+childList[i].menuName+'</button>'
				$.cookie("menuUrl", childList[i].menuUrl, {path:"/"});
				if (sonMenuUrl != null && sonMenuUrl != "") {
					initIframe(sonMenuUrl);	
				}else{
					initIframe(childList[i].menuUrl);
				}
			}else {
				html += '<button type="button" data-href="'+childList[i].menuUrl+'" class="btn btn-default">'+childList[i].menuName+'</button>';
			}
			html+= '</div>'
		} else {
			if (childList[i].selected == 1){
				html += '<li role="presentation" class="active"><a data-href="'+childList[i].menuUrl+'" href="javascript:void(0)">'+childList[i].menuName+'</a></li>'
				$.cookie("menuUrl", childList[i].menuUrl, {path:"/"});
				if (sonMenuUrl != null && sonMenuUrl != "") {
					initIframe(sonMenuUrl);	
				}else{
					initIframe(childList[i].menuUrl);
				}
			}else {
				html += '<li role="presentation"><a data-href="'+childList[i].menuUrl+'" href="javascript:void(0)">'+childList[i].menuName+'</a></li>';
			}
		}
	}
	if (menuUrl.indexOf("myCenter") != -1) {
		$(".top-tab").hide();
		$(".btn-tab").show();
		$(".main-content").css("top", "85px");
		$(".btn-tab").html(html);
	} else {
		$(".btn-tab").hide();
		$(".top-tab").show();
		if($(window).width()<768){
			$(".main-content").css("top", "51px");
		}else{
			$(".main-content").css("top", "41px");
		}
		
		$(".top-tab").html(html);
	}
}
function createPhoneTwoMenu(childList, select){
	var ul = '<li class="two-menu"><ul class="list-group">'
	for (var i = 0; i < childList.length; i++) {
		if (childList[i].menuUrl.indexOf("contentLibrary") != -1 || childList[i].menuUrl.indexOf("contentManage") != -1 || childList[i].menuUrl.indexOf("deepAnalysis") != -1) {
			continue;
		}
		if (childList[i].selected == 1 && select){
			ul += '<li class="list-two-item selected"><a data-href="'+childList[i].menuUrl+'" href="javascript:void(0)"><img src="'+childList[i].menuImage+'" />'+childList[i].menuName+'</a></li>';
			$.cookie("menuUrl", childList[i].menuUrl, {path:"/"});
			if (sonMenuUrl != null && sonMenuUrl != "") {
				initIframe(sonMenuUrl);	
			}else{
				if (childList[i].menuUrl.indexOf("createContent") != -1) {
					initIframe("myExtension/createPhoneContent.html");
				}else if (childList[i].menuUrl.indexOf("advertManage") != -1) {
					initIframe("myExtension/advertManagePhone.html");
				}else{
					initIframe(childList[i].menuUrl);	
				}
			}
		}else {
			ul += '<li class="list-two-item"><a data-href="'+childList[i].menuUrl+'" href="javascript:void(0)"><img src="'+childList[i].menuImage+'" />'+childList[i].menuName+'</a></li>';
		}
	}
	ul += '</ul></li>';
	return ul;
}

function createIframe(path){
	$(".btn-tab").hide();
	$(".top-tab").hide();
	if($(window).width()<768){
		$(".main-content").css("top", "51px");
	}else{
		$(".main-content").css("top", "0");
	}
//	history.pushState(null, null, "?path="+path+"&jump=1&topMenu=1");
	initIframe(path);
}

//初始化菜单
var initIframe = function(path){
	if (path.indexOf("?") != -1) {
		path = path + "&v="+new Date().getTime();
	}else{
		path = path + "?v="+new Date().getTime();
	}
	var html = '<iframe scrolling="yes" frameborder="0" marginwidth="0" marginheight="0" src="'+path+'"></iframe>';
	$(".main-content").find(".show_iframe").html(html);
}

//创建功能列表
function createButtonMenu(buttonList, sonMenuUrl){
	$(".btn-tab").show();
	var html = '';
	for (var i = 0; i < buttonList.length; i++) {
		html += '<div class="btn-group" role="group">'
		if (buttonList[i].selected == 1){
			html += '<button type="button" panel-href="'+item.url+'" class="btn btn-default active">'+item.name+'</button>'
		}else {
			html += '<button type="button" panel-href="'+item.url+'" class="btn btn-default">'+item.name+'</button>';
		}
		html+= '</div>'
	}
	$(".btn-tab").html(html);
}
